﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows.Forms;


namespace WMP
{
    public enum State
    {
        PLAY, PAUSE, STOP, ENDED, NOTHING
    };

    public class ID3V1
    {

        //ID3V1
        public char[] ident = new char[3];
        public char[] artist = new char[30];
        public char[] title = new char[30];
        public char[] album = new char[30];
        public char[] year = new char[4];
        public char[] genre = new char[1];
        //may change
        public char[] comment = new char[30];
        
        //ID3V11 only
        int trackNumber;
        

        public void readInfo(string file)
        {
            StreamReader stream = new StreamReader(file);
            int size = 0;

            while (stream.Read(ident, 0, 1) == 1)
                size += 1;

            stream.Close();
            stream = new StreamReader(file);

            size -= 127;
            while (size > 1)
            {
                stream.Read(ident, 0, 1);
                size -= 1;
            }

            stream.Read(ident, 0, 3);
            stream.Read(artist, 0, 30);
            stream.Read(title, 0, 30);
            stream.Read(album, 0, 30);
            stream.Read(year, 0, 4);
            stream.Read(comment, 0, 30);

            if (comment[28] == 0)
                trackNumber = comment[29];
            else
                trackNumber = -1;
            stream.Read(genre, 0, 1);

            string tmp = new string(ident);
            if (ident[0] != 'T' || ident[1] != 'A' || ident[2] != 'G')
                return;
            string tmp1 = new string(artist);
            string tmp2 = new string(title);
            string tmp3 = new string(album);
            string tmp4 = new string(year);
            string tmp5 = new string(comment);

            //MessageBox.Show(tmp + " caca " + tmp1 + " caca " + tmp2 + " caca " + tmp3 + " caca " + tmp4 + " caca " + tmp5);
            //MessageBox.Show(tmp5);
            stream.Close();
        }
    }

    public class ID3V2
    {

        //header;
        private char[] tag = new char[3];
        private char[] majver = new char[1];
        private char[] minver = new char[1];
        private char[] flag = new char[1];
        private char[] size = new char[4];

        //headertag;
        private char[] name;
        private char[] sizetag;
        private char[] flagTag;

        private bool vertion;
        private StreamReader stream;
        private ulong trueSize;
        private char[] info;
        private List<Info> list = new List<Info>();

        public struct Info
        {
            public string tipe;
            public short len;
            public char[] info;
        }

        public List<Info> getList()
        {
            return (list);
        }

        public void readInfo(string file)
        {
            stream = new StreamReader(file);
            
            stream.Read(tag, 0, 3);
            stream.Read(majver, 0, 1);
            stream.Read(minver, 0, 1);
            stream.Read(flag, 0, 1);
            stream.Read(size, 0, 4);
            majver[0] += '0';
            minver[0] += '0';
            trueSize = size[3] * (ulong)Math.Pow(16, 0);
            trueSize += size[2] * (ulong)Math.Pow(16, 1);
            trueSize += size[1] * (ulong)Math.Pow(16, 2);
            trueSize += size[0] * (ulong)Math.Pow(16, 4);

            if (majver[0] - '0' >= 3)
                vertion = true;
            else
                vertion = false;
            //MessageBox.Show(tag[0] + "" + tag[1] + "" + tag[2] + " " + majver[0] + "." + minver[0] + " " + trueSize.ToString());            
            if (vertion)
                while (readNextTag3())
                {
                }
            else
                while (readNextTag2())
                {
                }            
        }

        public bool readNextTag2()
        {
            int trueSizeTag;

            name = new char[3];
            sizetag = new char[3];

            trueSize -= 6;
            stream.Read(name, 0, 3);
            stream.Read(sizetag, 0, 3);
            trueSizeTag = sizetag[2] * (int)Math.Pow(16, 0);
            trueSizeTag += sizetag[1] * (int)Math.Pow(16, 1);
            trueSizeTag += sizetag[0] * (int)Math.Pow(16, 2);
            //MessageBox.Show("BITE");
            //MessageBox.Show(name[0] + "" + name[1] + "" + name[2] + " " + trueSizeTag.ToString());
            if (trueSizeTag == 0)
                return (true);
            string plop2 = new string(name);

            //MessageBox.Show("tagname " + plop2 + " size " + trueSizeTag.ToString());

            info = new char[trueSizeTag];
            stream.Read(info, 0, (int)trueSizeTag);

            if (plop2 == "TPE1" || plop2 == "TALB" || plop2 == "TIME" || plop2 == "TIT2")
            {
                Info inflist = new Info();
                if (plop2 == "TPE1")
                    inflist.tipe = "artist";
                if (plop2 == "TALB")
                    inflist.tipe = "album";
                if (plop2 == "TIME")
                    inflist.tipe = "duree";
                if (plop2 == "TIT2")
                    inflist.tipe = "name";
                inflist.len = (short)trueSizeTag;
                inflist.info = info;
                list.Add(inflist);
            }


            //if (trueSizeTag < 255)

            string plop = new string(info);
            //MessageBox.Show(plop + "");

            name = null;
            sizetag = null;
            flagTag = null;
            plop = null;
            plop2 = null;
            info = null;
            //MessageBox.Show(plop + "");

            if (trueSize >= (ulong)trueSizeTag)
            {
                trueSize -= (ulong)trueSizeTag;
                //MessageBox.Show(trueSize.ToString());
                return (true);
            }
            else
                return (false);
        }

        public bool readNextTag3()
        {
            ulong trueSizeTag;

            trueSize -= 10;

            name = new char[4];
            sizetag = new char[4];
            flagTag = new char[2];

            stream.Read(name, 0, 4);
            stream.Read(sizetag, 0, 4);
            stream.Read(flagTag, 0, 2);
            trueSizeTag = sizetag[3] * (ulong)Math.Pow(16, 0);
            trueSizeTag += sizetag[2] * (ulong)Math.Pow(16, 1);
            trueSizeTag += sizetag[1] * (ulong)Math.Pow(16, 2);
            trueSizeTag += sizetag[0] * (ulong)Math.Pow(16, 4);

            if (trueSizeTag == 0)
                return (true);
            string plop2 = new string(name);

            //MessageBox.Show("tagname " + plop2 + " size " + trueSizeTag.ToString());

            info = new char[trueSizeTag];
            stream.Read(info, 0, (int)trueSizeTag);

            if (plop2 == "TPE1" || plop2 == "TALB" || plop2 == "TIME")
            {
                Info inflist = new Info();
                if (plop2 == "TPE1")
                    inflist.tipe = "artist";
                if (plop2 == "TALB")
                    inflist.tipe = "album";
                if (plop2 == "TIME")
                    inflist.tipe = "duree";
                inflist.len = (short)trueSizeTag;
                inflist.info = info;
                list.Add(inflist);
            }


            //if (trueSizeTag < 255)

            string plop = new string(info);
            //MessageBox.Show(plop + "");

            name = null;
            sizetag = null;
            flagTag = null;
            plop = null;
            plop2 = null;
            info = null;
            //MessageBox.Show(plop + "");

            if (trueSize >= trueSizeTag)
            {
                trueSize -= trueSizeTag;
                //MessageBox.Show(trueSize.ToString());
                return (true);
            }
            else
                return (false);
        }
    }

    public class Tmp
    {
        public  ID3V2 convFromBuff(char[] buff, int size)
        {
            ID3V2 logFont = new ID3V2();
            IntPtr ptPoit = Marshal.AllocHGlobal(size);
            Marshal.Copy(buff, 0, ptPoit, size);
            /*logFont = */Marshal.PtrToStructure(ptPoit, logFont);
            Marshal.FreeHGlobal(ptPoit);
            return logFont;
        }

        /*public static char[] LogfontTocharArray(LogFont logFont)
        {
            IntPtr ptPoint = Marshal.AllocHGlobal(92);
            Marshal.StructureToPtr(logFont, ptPoint, true);
            char[] bff = new char[92];
            Marshal.Copy(ptPoint, bff, 0, 92);
            Marshal.FreeHGlobal(ptPoint);
            return bff;
        }*/
    }
}
